package com.dqhl.business.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.business.Base.BaseActivity;
import com.dqhl.business.R;
import com.dqhl.business.adapter.CouponsAdapter;
import com.dqhl.business.modle.Coupon;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Constant;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 优惠券Actibityz
 * autour: guanluocang
 * date: 2016/11/7 14:08
 */
public class CouponsActivity extends BaseActivity implements View.OnClickListener, CouponsAdapter.OnOperationListener {

    private Context context;
    private TextView tv_goodsTopTitle;
    private ImageView iv_goodsGoBack;
    private ImageView iv_addCoupon;
    private ListView lv_coupon;
    private TextView tv_no_data;
    private List<Coupon> couponList;
    private CouponsAdapter mCouponsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);
        context = this;
        initView();
        initData();

        registerBroadcastReceiver();
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constant.ACTION_ADD_COUPON)) {
                initData();
            }
        }
    };

    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACTION_ADD_COUPON);
        registerReceiver(receiver, intentFilter);
    }

    private void initView() {
        tv_goodsTopTitle = (TextView) findViewById(R.id.tv_goodsTopTitle);
        tv_goodsTopTitle.setText("优惠券设置");
        iv_goodsGoBack = (ImageView) findViewById(R.id.iv_goodsGoBack);
        lv_coupon = (ListView) findViewById(R.id.lv_coupon);
        iv_addCoupon = (ImageView) findViewById(R.id.iv_addCoupon);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);

        iv_addCoupon.setOnClickListener(this);
        iv_goodsGoBack.setOnClickListener(this);
    }

    private void initData() {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.personal_coupon_list);
        params.addBodyParameter("seller_id", user.getUser_id());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getData(result);
                if (0 == errCode) {
                    tv_no_data.setVisibility(View.GONE);
                    lv_coupon.setVisibility(View.VISIBLE);

                    couponList = JSON.parseArray(data, Coupon.class);
                    mCouponsAdapter = new CouponsAdapter(context, couponList, CouponsActivity.this);
                    lv_coupon.setAdapter(mCouponsAdapter);
                    lv_coupon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Bundle bundle = new Bundle();
                            bundle.putString("coupon_id", couponList.get(i).getId());
                            overlay(CouponDetailActivity.class, bundle);
                        }
                    });
                } else {
                    tv_no_data.setVisibility(View.VISIBLE);
                    lv_coupon.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_addCoupon:
                overlay(AddCouponActivity.class);
                break;
            case R.id.iv_goodsGoBack:
                finish();
                break;
        }
    }

    //修改优惠券
    @Override
    public void modifyCoupon(int position) {
        String coupon_id = couponList.get(position).getId();
        Dlog.e(coupon_id);
        Bundle bundle = new Bundle();
        bundle.putString("coupon_id", coupon_id);
        overlay(ModifyCouponActivity.class, bundle);
    }

    //删除优惠券
    @Override
    public void deleteCoupon(int position) {
        String coupon_id = couponList.get(position).getId();
        Dlog.e(coupon_id);
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.personal_coupon_delete);
        params.addBodyParameter("coupon_id", coupon_id);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                if (0 == errCode) {
                    initData();
                }
                toast(errMsg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    //发布优惠券
    @Override
    public void releaseCoupon(int position) {
        String coupon_id = couponList.get(position).getId();
        Dlog.e(coupon_id);
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.personal_coupon_publish);
        params.addBodyParameter("coupon_id", coupon_id);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                if (0 == errCode) {
                    initData();
                }
                toast(errMsg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}
